#!/bin/bash
if [ $# -ne 2 ];then
    echo "Usage:$0 dir1 dir2"
    exit 1
fi
if [ ! -d $1 -o ! -d $2 ];then
    echo "$1 or $2 is not derectory!"
    exit 1
fi

## 注意，Mac的readlink程序和GNU readlink功能不同，Mac需要下载greadlink
arg1=`greadlink -f $1`
arg2=`greadlink -f $2`

tmp_dir=/tmp/tmp.$$
rm -rf $tmp_dir
mkdir -p $tmp_dir || exit 0
#echo $tmp_dir

trap "rm -rf $tmp_dir; exit 0" SIGINT SIGTERM

## 注意，Mac和Linux的MD5程序不同，请根据需求使用，这里是Mac版的用法
function get_file_md5
{
    if [ $# -ne 1  ];then
        echo "get_file_md5 arg num error!"
        return 1
    fi
    local file=$1
    md5 $file | awk -F"=" '{print $2}'
}

function myexit
{
    rm -rf $tmp_dir
    exit 0
}

function show_diff
{
    if [ $# -ne 1 ];then
        return 1
    fi
    local diff_file=$1
    echo "diff file:"
    printf "    %-55s  %-52s\n" $arg1 $arg2
    if [ -f $tmp_dir/A_ony_file ];then
        awk '{printf("    [%2d] %-50s\n", NR, $1)}' $tmp_dir/A_ony_file
        python -c 'print "-"*100'
    fi
    awk '{printf("    [%2d] %-50s  %-50s\n", NR, $1, $1)}' $diff_file
    echo "(s):show diff files (a):open all diff files (q):exit"
    echo
}

function check_value
{
    local diff_file=$1
    local value=$2
    tmp_file=$tmp_dir/tmp_file
    >$tmp_file
    for numbers in `echo "$value" | tr ',' ' '`
    do
        nf=`echo "$numbers" | awk -F"-" '{print NF}'`
        if [ $nf -ne 1 -a $nf -ne 2 ];then
            return 1
        fi
        begin=`echo "$numbers" | awk -F"-" '{print $1}'`
        end=`echo "$numbers" | awk -F"-" '{print $2}'`
        if [ -z "$end" ];then
            sed -n $begin'p' $diff_file >> $tmp_file
        else
            if [ "$end" -lt $begin ];then
                return 1
            fi
            sed -n $begin','$end'p' $diff_file >> $tmp_file
        fi
        if [ $? -ne 0 ];then
            return 1
        fi
    done
    awk -v dir1=$arg1 -v dir2=$arg2 '{
        if (NR==1)
        {
            printf("edit %s/%s\nvertical diffsplit %s/%s\n", dir1, $0, dir2, $0)
        }
        else
        {
            printf("tabnew %s/%s\nvertical diffsplit %s/%s\n", dir1, $0, dir2, $0)
        }
    }' $tmp_file
}

#############################################################
# 获取diff info
#############################################################
for file in `find $arg1 | grep -v "/\." | grep -v "^\."`
do
    file_relative_name=${file#$arg1/}
    file $file | grep -Eq "text"
    if [ $? -ne 0 ];then
        continue
    fi
    if [ -f $arg2/$file_relative_name ];then
        file $arg2/$file_relative_name | grep -Eq "text"
        if [ $? -ne 0 ];then
            continue
        fi
        md5_1=`get_file_md5 $file`
        md5_2=`get_file_md5 $arg2/$file_relative_name`
        if [[ "$md5_1" = "$md5_2" ]];then
            continue
        fi
        ## file not same
        echo "$file_relative_name" >> $tmp_dir/diff_file
    else
        echo "$file_relative_name" >> $tmp_dir/A_ony_file
    fi
done

#############################################################
# 根据输入标签打开用vim打开文件比较diff
#############################################################
if [ ! -f $tmp_dir/diff_file ];then
    exit
fi

show_diff $tmp_dir/diff_file
while true
do
    echo -n "Please choose file number list (like this:1,3-4,5):"
    read value
    if [[ "$value" = "s" ]] || [[ "$value" = "S" ]];then
        show_diff $tmp_dir/diff_file
        continue
    elif [[ "$value" = "q" ]] || [[ "$value" = "Q" ]];then
        myexit
    elif [[ "$value" = "a" ]] || [ "$value" = "A" ];then
        value="1-$"
    fi
    vim_script=`check_value $tmp_dir/diff_file "$value" 2>/dev/null`
    if [ $? -ne 0 ];then
        echo "invalid parameter[$value]!"
    else
        vim -c "$vim_script"
    fi
done
